package com.homedo.core.common.util;

import com.homedo.core.common.base.dto.Meta;
import com.homedo.core.common.exception.BizException;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


public class DateUtils {

    public static final String FORMATE_DAY = "yyyy-MM-dd";

    /**
     * 根据传入的日期格式化输出
     * {formatStr} 的字符串时间
     *
     * @param date      时间
     * @param formatStr yyyy-MM-dd等时间串
     * @return
     */
    public static String formatDate(Date date, String formatStr) {
        SimpleDateFormat formatter = new SimpleDateFormat(formatStr);
        String outDate = formatter.format(date);
        return outDate;
    }

    /**
     * 根据传入的日期格式化输出yyyy-MM-dd 的字符串时间
     *
     * @param date      时间
     * @return
     */
    public static String formatDate(Date date) {
        return formatDate(date, FORMATE_DAY);
    }

    /**
     * 格式化日期，格式：yyyy-MM-dd
     * @param dateStr
     * @return
     */
    public static Date parseDate(String dateStr) {
        return parseDate(dateStr, FORMATE_DAY);
    }

    public static Date parseDate(String dateStr, String format) {
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat(format);
        try {
            return simpleDateFormat.parse(dateStr);
        } catch (ParseException e) {
            throw new BizException(Meta.CODE_451.getCode(),"日期解析出错 :"+dateStr);
        }
    }

    /**
     * 得到对应格式时间，格式："yyyy-MM-dd"
     * @param date
     * @return
     */
    public static Date parseDate(Date date) {
        return parseDate(date, FORMATE_DAY);
    }

    /**
     * 得到对应格式时间
     * @param date
     * @return
     */
    public static Date parseDate(Date date, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        try {
            String dStr = sdf.format(date);
            return sdf.parse(dStr);
        } catch (Exception e) {
            return null;
        }
    }

    public static boolean compareLongTime(Date startDate, Date endDate) {
        if(startDate.getTime()>endDate.getTime()){
            throw new BizException(Meta.CODE_451.getCode(),"开始时间大于结束时间");
        }
        return true;
    }

    public static boolean compareLongTime(Long startTime, Long endTime) {
        if(startTime == null || endTime == null){
            throw new BizException(Meta.CODE_451.getCode(),"开始 结束时间非法");
        }
        if(startTime < 0 || endTime < 0){
            throw new BizException(Meta.CODE_451.getCode(),"开始 结束时间非法");
        }
        if(startTime>endTime){
            throw new BizException(Meta.CODE_451.getCode(),"时间区间不合法");
        }
        return true;
    }



    public static Date beforeDate(Date date,int before) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        int i = c.get(Calendar.DATE);
        c.set(Calendar.DATE,i-before);
        return c.getTime();
    }

    public static Date getDate(Date date,String formatStr) {
        try {
            DateFormat df= new SimpleDateFormat(formatStr);
            String format = df.format(date);
            return df.parse(format);
        } catch (ParseException e) {
            throw new BizException(Meta.CODE_451.getCode(),"获取date失败");
        }
    }

    public static Date getLastDate() {
        Date nowDate = parseDate(new Date());
        Calendar c = Calendar.getInstance();
        c.setTime(nowDate);
        c.set(Calendar.DATE, c.get(Calendar.DATE) - 1);
        return parseDate(c.getTime());
    }

    public static Map<String,Date> getLastWeek() {
        Map<String, Date> map = new HashMap<String, Date>();
        Calendar cal = Calendar.getInstance();
        int n = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (n == 0) {
            n = 7;
        }
        // 上周一的日期
        cal.add(Calendar.DATE, -(7 + (n - 1)));
        Date monday = cal.getTime();
        monday = parseDate(monday);
        map.put("monday", monday);

        cal.add(Calendar.DATE, 1);
        Date tuesday = cal.getTime();
        tuesday = parseDate(tuesday);
        map.put("tuesday", tuesday);

        cal.add(Calendar.DATE, 1);
        Date wednesday = cal.getTime();
        wednesday = parseDate(wednesday);
        map.put("wednesday", wednesday);

        cal.add(Calendar.DATE, 1);
        Date thursday = cal.getTime();
        thursday = parseDate(thursday);
        map.put("thursday", thursday);

        cal.add(Calendar.DATE, 1);
        Date friday = cal.getTime();
        friday = parseDate(friday);
        map.put("friday", friday);

        cal.add(Calendar.DATE, 1);
        Date saturday = cal.getTime();
        saturday = parseDate(saturday);
        map.put("saturday", saturday);

        cal.add(Calendar.DATE, 1);
        Date sunday = cal.getTime();
        sunday = parseDate(sunday);
        map.put("sunday", sunday);
        return map;
    }

    public static String getBeforeFirstMonthdate() {
        SimpleDateFormat format = new SimpleDateFormat(FORMATE_DAY);
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, -1);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        return format.format(calendar.getTime());
    }

    public static String getBeforeLastMonthdate() {
        SimpleDateFormat sf = new SimpleDateFormat(FORMATE_DAY);
        Calendar calendar = Calendar.getInstance();
        int month = calendar.get(Calendar.MONTH);
        calendar.set(Calendar.MONTH, month - 1);
        calendar.set(Calendar.DAY_OF_MONTH,
                calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        return sf.format(calendar.getTime());
    }

    /**
     * 获取格式化后的日期字符串
     * @param date
     * @return
     */
    public static String getDateString(Date date, String format) {
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        return formatter.format(date);
    }

    /**
     * 获取格式化后的日期字符串，格式：yyyy-MM-dd
     * @param date
     * @return
     */
    public static String getDateString(Date date) {
        return getDateString(date, FORMATE_DAY);
    }


    public static void main(String[] args) {
        System.out.println(DateUtils.beforeDate(new Date(),3));

        System.out.println(DateUtils.getDate(new Date(),"yyyy-MM-dd"));
    }

}
